using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Xml;

namespace Ent.Web.Helper
{
    public enum SiteMapMenus
    {
        Admin, Secure, Public, NotSet
    }
    public class SiteMenuDataSource : XmlDataSource
    {
        public string Language = "";
        public bool IsPageDisabled(string pagename)
        {
            XmlNode node = this.GetXmlDocument().SelectSingleNode("//siteMapNode[contains(@url,'/" + pagename + "')]/@disable");
            if (node != null)
                return bool.Parse(node.Value);
            else
                return false;
        }
        public string GetSiteDescription(string pagename)
        {
            XmlNode node = this.GetXmlDocument().SelectSingleNode("//siteMapNode[contains(@url,'/" + pagename + "')]/@description");
            if (node != null)
                return node.Value;
            else
                return "";
        }
    }
    public class SiteMapHelper
    {
        static Dictionary<SiteMapMenus, SiteMenuDataSource> menuCache = new Dictionary<SiteMapMenus, SiteMenuDataSource>();
        public static SiteMenuDataSource GetMenuDataSource(SiteMapMenus menu, string serverMapPath, string language)
        {
            if (menuCache.ContainsKey(menu))
            {
                SiteMenuDataSource dataSource = menuCache[menu];
                if (dataSource.Language.ToLower() == language.ToLower())
                    return dataSource;
            }
            SiteMenuDataSource objData = new SiteMenuDataSource();
            objData.XPath = "siteMap/siteMapNode";
            switch (menu)
            {
                case SiteMapMenus.Admin:
                    if (System.IO.File.Exists(serverMapPath + @"\Data\Menu\Admin_" + language + ".sitemap"))
                        objData.DataFile = serverMapPath + @"\Data\Menu\Admin_" + language + ".sitemap";
                    else
                        objData.DataFile = serverMapPath + @"\Data\Menu\Admin_en-US.sitemap";
                    break;
                case SiteMapMenus.Secure:
                    if (System.IO.File.Exists(serverMapPath + @"\Data\Menu\Secure_" + language + ".sitemap"))
                        objData.DataFile = serverMapPath + @"\Data\Menu\Secure_" + language + ".sitemap";
                    else
                        objData.DataFile = serverMapPath + @"\Data\Menu\Secure_en-US.sitemap";
                    break;
                case SiteMapMenus.Public:
                    if (System.IO.File.Exists(serverMapPath + @"\Data\Menu\Public_" + language + ".sitemap"))
                        objData.DataFile = serverMapPath + @"\Data\Menu\Public_" + language + ".sitemap";
                    else
                        objData.DataFile = serverMapPath + @"\Data\Menu\Public_en-US.sitemap";
                    break;
                default:
                    break;
            }
            objData.DataBind();
            objData.Language = language;
            if (menuCache.ContainsKey(menu))
                menuCache[menu] = objData;
            else
                menuCache.Add(menu, objData);
            return objData;
        }
    }
}
